<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addFn"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" width="120">
                <template #default="{$index}">
                  <!-- 真正的序号应该是
                  之前页面(前一页)的所有数据长度开始 +1 +2 +3 ... -->
                  {{ (page.page-1) * page.pagesize + $index + 1 }}
                </template>
              </el-table-column>
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="compileFn(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="page.total"
                :page-size="page.pagesize"
                :current-page="page.page"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="别想改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

      </el-card>
      <el-dialog :title="`${dialogTitle}角色`" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :data="permList"
          :props="{label:'name'}"
          :show-checkbox="true"
          :check-strictly="true"
          :default-expand-all="true"

          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  delCompanyInfo,
  getRoleDetail,
  rolecompileFn,
  addRoleDetail,
  assignPermission

} from '@/api/setting'
import { getpermission } from '@/api/permission'
// import { mapGetters } from '@/store/'
import { listToTree } from '@/utils'
export default {
  data() {
    return {
      permList: [],
      showPermDialog: false,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      list: [], // 承接数组
      formData: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 2,
        total: 0 // 记录总数
      }

    }
  },
  computed: {
    dialogTitle() {
      return this.roleForm.id ? '编辑' : '新增'
    }
  },

  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo()// 获取公司信息
    this.getpermission()// 获取权限点
  },
  methods: {
    async getpermission() {
      // 获取权限点
      const res = await getpermission()
      // 然后过滤后面的
      this.permList = listToTree(res.filter(e => e.enVisible === '1'), '0')
    },
    // 分配权限
    async assignPerm(id) {
      this.roleId = id
      // 获取角色详情
      const { permIds } = await getRoleDetail(id)
      // 打开弹窗
      this.showPermDialog = true
      // 设置每个人的权限
      this.$nextTick(() => {
        // console.log(permIds)
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    // 分配角色权限确定按钮
    async btnPermOK() {
      const data = {
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys()
      }
      // 请求
      await assignPermission(data)
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.showPermDialog = false
    },
    // 分配角色权限取消按钮
    btnPermCancel() {
      // 清空数据
      this.$refs.permTree.setCheckedKeys([])
      // 关闭弹窗
      this.showPermDialog = false
    },
    async getRoleList() {
      // 获取角色列表
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    async getCompanyInfo() {
      // 获取公司信息
      this.formData = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
      // console.log(this.formData)
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      // 再发请求重新获取数据
      this.getRoleList()
    },
    // 删除按钮
    async delFn(id) {
      try {
        await this.$confirm('是否确认')
        await delCompanyInfo(id)
        // 提示成功
        if (this.list.length === 1 && this.page.page > 1) {
          this.page.page--
        }
        this.getRoleList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑按钮
    async compileFn(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    // 确定/提交按钮
    async btnOK() {
      try {
        // 表单效验
        this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          // 获取数据
          await rolecompileFn(this.roleForm)
        } else {
          // 没有id就新增角色
          await addRoleDetail(this.roleForm)
        }
        // 提示成功
        this.$message.success('操作成功')
        // 关闭弹窗
        this.showDialog = false
        // 更新数据
        await this.getRoleList()
        this.page.page = Math.ceil(this.page.total / this.page.pagesize)
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 取消按钮
    btnCancel() {
      // 清空初始化
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除效验
      this.$refs.roleForm.resetFields()
      // 弹窗为false
      this.showDialog = false
    },
    // 新增功能
    async addFn() {
      this.showDialog = true
    }
  }
}
</script>

<style>

</style>
